#include "nastavenisit.h"

pageNastaveniSit::pageNastaveniSit(QWidget *parent) :
    QWidget(parent)
{
    //Popisky
    QLabel * popisekJmeno=new QLabel("Jméno",this,Qt::Widget);
    QLabel * popisekSymbol=new QLabel("Symbol",this,Qt::Widget);
    QLabel * popisekBarva=new QLabel("Barva",this,Qt::Widget);
    QLabel * popisekAdresa = new QLabel("IP adresa",this, Qt::Widget);
    QLabel * popisekPort = new QLabel("port",this, Qt::Widget);

    //Ostatni
    jmenoHrace1 = new QLineEdit(this);
    symbolHrace1 = new QComboBox(this);

    symbolHrace1->addItem("Křížky",0);
    symbolHrace1->addItem("Kolečka",1);

    zvolBarvu1 = new QtColorPicker(this);
    zvolBarvu1->setStandardColors();
    zvolBarvu1->setCurrentColor(Qt::blue);

    ipAdresa = new QLineEdit("127.0.0.1",this);
    port = new QLineEdit("6666",this);

    QPushButton * pbVytvorit = new QPushButton("Vytvořit hru",this);
    QPushButton * pbPripojit = new QPushButton("Připojit se",this);

    connect(pbVytvorit,SIGNAL(clicked()),this,SLOT(on_pbVytvorit_clicked()));
    connect(pbPripojit,SIGNAL(clicked()),this,SLOT(on_pbPripojit_clicked()));


    //Layouty
    QHBoxLayout *hbox1 = new QHBoxLayout();
    QHBoxLayout *hbox2 = new QHBoxLayout();
    QHBoxLayout *hbox3 = new QHBoxLayout();
    QHBoxLayout *hbox4 = new QHBoxLayout();
    QHBoxLayout *hbox5 = new QHBoxLayout();
    QVBoxLayout *vbox1 = new QVBoxLayout(this);


    //Nastaveni layoutu

    hbox1->setSpacing(30);

    hbox1->addWidget(popisekJmeno);
    hbox1->addWidget(jmenoHrace1);

    hbox2->setSpacing(30);

    hbox2->addWidget(popisekSymbol);
    hbox2->addWidget(symbolHrace1);

    hbox3->setSpacing(30);

    hbox3->addWidget(popisekBarva);
    hbox3->addWidget(zvolBarvu1);

    hbox4->addWidget(popisekAdresa);
    hbox4->addWidget(ipAdresa);

    hbox4->addWidget(popisekPort);
    hbox4->addWidget(port);

    hbox5->addWidget(pbVytvorit);
    hbox5->addWidget(pbPripojit);

    vbox1->setSpacing(20);
    vbox1->addLayout(hbox1);
    vbox1->addLayout(hbox2);
    vbox1->addLayout(hbox3);
    vbox1->addLayout(hbox4);
    vbox1->addLayout(hbox5);

    this->setLayout(vbox1);
}

Hraci * pageNastaveniSit::vratHrace()
{
    return hraci;
}

void pageNastaveniSit::on_pbVytvorit_clicked()
{
    hraci = new Hraci();
    hraci->vratHrace(0)->nastavVlastnosti(jmenoHrace1->text(), zvolBarvu1->currentColor(), symbolHrace1->currentIndex(), 0, LOCAL);
    tcpServer = new QTcpServer();
    tcpServer->setProxy(QNetworkProxy::NoProxy);
    //if (!tcpServer->listen(QHostAddress(ipAdresa->text()), port->text().toInt())) {
    if (!tcpServer->listen(QHostAddress::Any, port->text().toInt())) {
        QMessageBox::critical(this, tr("Pisqorky"),"Server se nepodařilo spustit.");
    } else { QMessageBox::information(this, tr("Pisqorky"),QString("Server byl spuštěn na adrese: %1:%2.")
                                      .arg(ipAdresa->text(), port->text()));
    }
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(onClientConnect()));
}

void pageNastaveniSit::on_pbPripojit_clicked()
{
    hraci = new Hraci();
    hraci->vratHrace(0)->nastavVlastnosti(jmenoHrace1->text(), zvolBarvu1->currentColor(), symbolHrace1->currentIndex(), 0, LOCAL);
    tcpServer = NULL;
    tcpClient = new QTcpSocket();
    tcpClient->connectToHost(ipAdresa->text(), port->text().toInt());
    connect(tcpClient,SIGNAL(readyRead()), this, SLOT(onNewData()));
}

void pageNastaveniSit::onClientConnect()
{
    this->tcpClient = tcpServer->nextPendingConnection();
    connect(tcpClient,SIGNAL(readyRead()), this, SLOT(onNewData()));
    disconnect(this,SLOT(onClientConnect()));
    QByteArray tmp;
    QDataStream out(&tmp, QIODevice::WriteOnly);
    hraci->vratHrace(0)->serialize(out);
    tmp.prepend("HI;");
    this->tcpClient->write(tmp);
    this->tcpClient->flush();
}

void pageNastaveniSit::onNewData()
{
    QTcpSocket* socket = qobject_cast<QTcpSocket*>(this->sender());
    QByteArray rawdata = socket->readAll();
    QList<QByteArray> args = rawdata.split(';');
    QString command = args.takeFirst();
    QDataStream stream(args.takeFirst());
    Hrac * humm = new Hrac(stream);
    hraci->vratHrace(1)->nastavVlastnosti(*humm->zjistiJmeno(),*humm->zjistiBarvu(),*humm->zjistiZnak(),*humm->zjistiBody(), REMOTE);
    disconnect(this,SLOT(onNewData()));
    if(tcpServer == NULL)
    {
        hraci->zmenAktualniho();
        QByteArray tmp;
        QDataStream out(&tmp, QIODevice::WriteOnly);
        hraci->vratHrace(0)->serialize(out);
        tmp.prepend("HI;");
        this->tcpClient->write(tmp);
        this->tcpClient->flush();
    }
    this->parentWidget()->parentWidget()->parentWidget()->close();
}
